<HTML><HEAD></SCRIPT></HEAD><BODY>
<?php

require('globals.php');
require('logger.php');

$values = array();
$query_array = array();

function LockAndLoad($index, $table) {

	global $query_array;

	switch($index) {
		case(1):
			$query_array[0] = $table . "A";
			$query_array[1] = $table . "B";
			break;
		case(2):
			$query_array[0] = substr($table,0,3) . "A";
			$query_array[1] = substr($table,0,3) . "B";
			$query_array[2] = substr($table,4,3) . "A";
			$query_array[3] = substr($table,4,3) . "B";
			break;
		case(3):
			$query_array[0] = substr($table,0,3) . "A";
			$query_array[1] = substr($table,0,3) . "B";
			$query_array[2] = substr($table,4,3) . "A";
			$query_array[3] = substr($table,4,3) . "B";
			$query_array[4] = substr($table,8,3) . "A";
			$query_array[5] = substr($table,8,3) . "B";
			break;
		case(4):
			$query_array[0] = substr($table,0,1) . substr($table,1,2) . "A";
			$query_array[1] = substr($table,0,1) . substr($table,1,2) . "B";
			break;
		default:
			$query_array[0] = $table;
		}
}

$dealer_update_query = "UPDATE " . $dealers_table . " SET ";
$id = $_POST[dealer_id];

if (!($link = mysql_connect($host, $user, $password)))
	die('Could not connect: ' . mysql_error());

if (!(mysql_select_db($database)))
	die('Could not select db ' . $database . ': ' . mysql_error());

foreach ($fieldnames_clean as $fieldnames_sub) {
	$key = $fieldnames_sub[0];
	$value = mysql_real_escape_string($_POST[$key], $link);
	if ($key != "assignment" && $key != "dealer_id" && $key != "cancelled_date") {
		//special cases--integers are required for these fields
		if ($value == '' && ($key == 'table_pref' ||
							 $key == 'wall' ||
							 $key == 'table_adjacent_to' ||
							 $key == 'table_away_from' ||
							 $key == 'table_no_request' ||
							 $key == 'requests' ||
							 $key == 'cancelled' ||
							 $key == 'power' ||
							 $key == 'is_notified' ||
							 $key == 'trouble')) {
			$dealer_update_query .= $key . "= '0', ";
		}
		else {
			if ($key == 'cancelled')
				$dealer_update_query .= $key . " = '" . $value . "', cancelled_date = '" . date("m/d/Y") . "', ";
			else
				$dealer_update_query .= $key . " = '" . $value . "', ";
		}
	}
}

//die($dealer_update_query);

// strip off the trailing comma and tack on the ending
$dealer_update_query = substr($dealer_update_query, 0, -3) . "' WHERE dealer_id = " . $id;

if (!(mysql_query($dealer_update_query, $link)))
	die('Could not execute query "' . $dealer_update_query . '": ' . mysql_error());

LockAndLoad($_POST[table_size_req], $_POST[assignment], $query_array);

//echo $assignment;
//echo(sizeof($query_array)-1);
//echo($_POST[assignment]);
//echo($query_array[0]);

for ($i=0;$i <= (sizeof($query_array)-1); $i++) {
	if (strlen($query_array[$i]) != 4) {
		//echo("Test");
		$table_reference = strtolower(substr($query_array[$i],4,1));
		$row_reference = strtolower(substr($query_array[$i],0,4));
		} else {
		$table_reference = strtolower(substr($query_array[$i],3,1));
		$row_reference = strtolower(substr($query_array[$i],0,3));
	}
	if ($table_reference) {
		//echo ("Table changes and Update Table option was on!" . "<BR>");
		$table_add_query = "UPDATE " . $tables_table . " SET row_" . $table_reference . "_assignment=" . $id .
				" WHERE row_number='" . $row_reference . "'";
		if (!(mysql_query($table_add_query, $link)))
		die('Could not execute query "' . $table_add_query . '": ' . mysql_error());
	//else echo ("Either no table changes or the Update Table option was off." . "<BR>");
	}
}

$values[0] = mysql_real_escape_string($_POST['dealership_name'], $link);
RecordLogEntry($values);

mysql_close($link);

?>
Success!<BR>
<INPUT type=button value="Return To Form" onClick="location.href = 'Edit Dealer.php?id=<?=$id?>';">
<INPUT type=button value="Return to Dealer List" onClick="location.href='Dealer List.php';">
</BODY></HTML>